//
//  BuyTicketViewController.m
//  EasyToo
//
//  Created by Jr Lan on 13-9-1.
//
//

#import "BuyTicketViewController.h"
#import "ConfirmCommonTicketViewController.h"
#import "LeveyTabBarController.h"
#import <QuartzCore/QuartzCore.h>

@interface BuyTicketViewController ()

@end

@implementation BuyTicketViewController
{
    UITextField *_countTF;
    UITextField *_phoneTF;
    
    UIButton *_zfbButton;
    UIButton *_ybButton;
    
    NSInteger _payType;
    UIAlertView *_baseAlert;
    
    CommonCinemaInfo *_cinemaInfo;
}

- (void)dealloc
{
    [_countTF release];
    [_phoneTF release];
    [_zfbButton release];
    [_ybButton release];
    [_cinemaInfo release];
    
    _countTF = nil;
    _phoneTF = nil;
    _zfbButton = nil;
    _ybButton = nil;
    _cinemaInfo = nil;
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        //查看通票
        _cinemaInfo = [[CommonCinemaInfo alloc] init];
        MovieManager *moviemanager = [MovieManager shareManager];
        moviemanager.delegate = self;
        [moviemanager qryCommonTicketInfo];

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//初始化视图
- (void)init_view
{
    self.title = @"电影票";
    //加载背影
     [self.view setBackgroundColor:BackgroundColor];
    
    
    int width = [DeviceUtil getScreenWidth];
    int heigth = self.view.frame.size.height;
    
    UIScrollView *myscrollview = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, heigth)]autorelease];
    myscrollview.directionalLockEnabled = YES; //只能一个方向滑动
    myscrollview.pagingEnabled = NO; //是否翻页
    myscrollview.backgroundColor = [UIColor whiteColor];
    myscrollview.showsVerticalScrollIndicator =YES; //垂直方向的滚动指示
    myscrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动指示的风格
    myscrollview.showsHorizontalScrollIndicator = NO;//水平方向的滚动指示
    myscrollview.delegate = self;
    CGSize newSize = CGSizeMake(width, 1000);
    [myscrollview setContentSize:newSize];
    
    UIControl *control = [[[UIControl alloc] initWithFrame:myscrollview.frame] autorelease];
    [myscrollview addSubview:control];
    [control addTarget:self action:@selector(onBackgroudTouch) forControlEvents:UIControlEventTouchDown];
    

    //加载影票信息
    [self initTicket:myscrollview];
    
    //加载图片
    UIImageView *desclimgView = [[[UIImageView alloc] initWithFrame:CGRectMake(10, ADJUST_IOS7STATUES_HIGHT+220, 20, 20)]autorelease];
    desclimgView.image = [UIImage imageNamed:@"4.png"];
    [myscrollview addSubview:desclimgView];
    
    //填写手机信息
    UILabel *phonelbl = [[[UILabel alloc]initWithFrame:CGRectMake(40, ADJUST_IOS7STATUES_HIGHT+220, 300, 20)]autorelease];
    phonelbl.text = @"填写手机信息";
    phonelbl.font = [UIFont systemFontOfSize:15];
    phonelbl.backgroundColor = [UIColor clearColor];
    phonelbl.adjustsFontSizeToFitWidth = YES;
    [myscrollview addSubview:phonelbl];
    
    //手机号码
    _phoneTF = [[UITextField alloc] initWithFrame:CGRectMake(20, ADJUST_IOS7STATUES_HIGHT+250, 280, 50)];
    _phoneTF.text = @"";
    _phoneTF.placeholder = @"请输入手机号码";
    _phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    _phoneTF.font = [UIFont fontWithName:@"Arial" size:18.0f];
    _phoneTF.textColor = [UIColor blackColor];
    _phoneTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _phoneTF.borderStyle = UITextBorderStyleRoundedRect;
    _phoneTF.delegate = self ;
    _phoneTF.returnKeyType =UIReturnKeyDone;
    _phoneTF.layer.borderColor = [UIColor grayColor].CGColor;
    _phoneTF.layer.borderWidth = 1 ;
    _phoneTF.layer.cornerRadius = 10 ;
    [myscrollview addSubview:_phoneTF];
    
    //提示信息
    UILabel *alertlbl = [[[UILabel alloc]initWithFrame:CGRectMake(10, ADJUST_IOS7STATUES_HIGHT+310, 300, 30)]autorelease];
    alertlbl.text = @"请确定手机号码无误，支付成功后将无法修改";
    alertlbl.font = [UIFont systemFontOfSize:15];
    alertlbl.backgroundColor = [UIColor clearColor];
    alertlbl.textColor = [UIColor lightGrayColor];
    alertlbl.adjustsFontSizeToFitWidth = YES;
    [myscrollview addSubview:alertlbl];
    
    UILabel *label_pay = [[[UILabel alloc]initWithFrame:CGRectMake(10, ADJUST_IOS7STATUES_HIGHT+350, 180, 30)]autorelease];
    label_pay.backgroundColor = [UIColor clearColor];
    label_pay.text = @"选择支付方式:";
    [myscrollview addSubview:label_pay];
    
//    _zfbButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 390, 100, 30)];
//    [_zfbButton setBackgroundImage:[UIImage imageNamed:@"zfb.png"] forState:UIControlStateNormal];
//    [_zfbButton setBackgroundImage:[UIImage imageNamed:@"zhifubao2.png"] forState:UIControlStateSelected];
//    [_zfbButton addTarget:self action:@selector(zfbButtonPick:) forControlEvents:UIControlEventTouchDown];
//    [myscrollview addSubview:_zfbButton];
    
    _ybButton = [[UIButton alloc] initWithFrame:CGRectMake(30, ADJUST_IOS7STATUES_HIGHT+390, 100, 30)];
    [_ybButton setBackgroundImage:[UIImage imageNamed:@"yinlian1"] forState:UIControlStateNormal];
    [_ybButton setBackgroundImage:[UIImage imageNamed:@"yinlian2"] forState:UIControlStateSelected];
    [_ybButton addTarget:self action:@selector(ybButtonPick:) forControlEvents:UIControlEventTouchDown];
    [myscrollview addSubview:_ybButton];
    
    
    UIButton *nextStep =[UIButton buttonWithType:UIButtonTypeCustom];
    nextStep.frame = CGRectMake(40, ADJUST_IOS7STATUES_HIGHT+430, 230, 40);
    [nextStep setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal];
    [nextStep setTitle:@"下一步" forState:UIControlStateNormal];
    nextStep.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    nextStep.layer.borderColor = [UIColor grayColor].CGColor;
    nextStep.layer.borderWidth = 1 ;
    nextStep.layer.cornerRadius = 10 ;
    [myscrollview addSubview:nextStep];
    [nextStep addTarget:self action:@selector(confirmMessge) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:myscrollview];
}

//加载影票信息
- (void) initTicket:(UIScrollView*)myscrollview
{
    //加载图片
    UIImageView *imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(10, ADJUST_IOS7STATUES_HIGHT+80, 300, 130)]autorelease];
    imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    imgView.image = [UIImage imageNamed:@"actingtickets_03"];
    [myscrollview addSubview:imgView];
    
    //加载说明
    UILabel *desclbl = [[[UILabel alloc]initWithFrame:CGRectMake(20, ADJUST_IOS7STATUES_HIGHT+80, 280, 30)]autorelease];
    desclbl.text = @"电子通票";
    desclbl.font = [UIFont systemFontOfSize:15];
    desclbl.backgroundColor = [UIColor clearColor];
    desclbl.adjustsFontSizeToFitWidth = YES;
    desclbl.textAlignment = NSTextAlignmentCenter;
    [myscrollview addSubview:desclbl];
    
    //加载影院
    UILabel *cinemalbl = [[[UILabel alloc]initWithFrame:CGRectMake(40, ADJUST_IOS7STATUES_HIGHT+110, 280, 30)]autorelease];
    cinemalbl.text = _cinemaInfo.cinemaname;
    cinemalbl.font = [UIFont systemFontOfSize:15];
    cinemalbl.backgroundColor = [UIColor clearColor];
    cinemalbl.adjustsFontSizeToFitWidth = YES;
    cinemalbl.textAlignment = NSTextAlignmentLeft;
    [myscrollview addSubview:cinemalbl];
    
    //加载类型
    UILabel *typelbl = [[[UILabel alloc]initWithFrame:CGRectMake(40, ADJUST_IOS7STATUES_HIGHT+140, 280, 30)]autorelease];
    typelbl.text = [NSString stringWithFormat:@"类型：%@（90天有效）", _cinemaInfo.tName];
    typelbl.font = [UIFont systemFontOfSize:15];
    typelbl.backgroundColor = [UIColor clearColor];
    typelbl.adjustsFontSizeToFitWidth = YES;
    typelbl.textAlignment = NSTextAlignmentLeft;
    [myscrollview addSubview:typelbl];
    
    //加载数量
    UILabel *countlbl = [[[UILabel alloc]initWithFrame:CGRectMake(40, ADJUST_IOS7STATUES_HIGHT+170, 40, 30)]autorelease];
    countlbl.text = @"数量：";
    countlbl.font = [UIFont systemFontOfSize:15];
    countlbl.backgroundColor = [UIColor clearColor];
    countlbl.adjustsFontSizeToFitWidth = YES;
    countlbl.textAlignment = NSTextAlignmentCenter;
    [myscrollview addSubview:countlbl];
    
    //加载加号
    UIButton *plusbtn =[UIButton buttonWithType:UIButtonTypeCustom];
    plusbtn.frame = CGRectMake(90, ADJUST_IOS7STATUES_HIGHT+175, 30, 25);
    [plusbtn setBackgroundImage:[UIImage imageNamed:@"15.png"] forState:UIControlStateNormal];
    [myscrollview addSubview:plusbtn];
    [plusbtn addTarget:self action:@selector(plusEvent) forControlEvents:UIControlEventTouchUpInside];
    
    //加载数量
    _countTF = [[UITextField alloc] initWithFrame:CGRectMake(130, ADJUST_IOS7STATUES_HIGHT+175, 40, 25)];
    _countTF.text = @"2";
    [_countTF setBorderStyle:UITextBorderStyleBezel];
    _countTF.backgroundColor = [UIColor whiteColor];
    [_countTF setEnabled:NO];
    [myscrollview addSubview:_countTF];
    
    //加载减号
    UIButton *minusbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    minusbtn.frame = CGRectMake(180, ADJUST_IOS7STATUES_HIGHT+175, 30, 25);
    [minusbtn setBackgroundImage:[UIImage imageNamed:@"16.png"] forState:UIControlStateNormal];
    [myscrollview addSubview:minusbtn];
    [minusbtn addTarget:self action:@selector(minusEvent) forControlEvents:UIControlEventTouchUpInside];
}


//数量增加
- (void)plusEvent
{
    _countTF.text = [NSString stringWithFormat:@"%d", ([_countTF.text intValue] + 1)];
}

//数量减少
- (void)minusEvent
{
    int count = [_countTF.text intValue];
    if (count > 0) {
        _countTF.text = [NSString stringWithFormat:@"%d", (count - 1)];
    }
}

-(void)zfbButtonPick:(id)sender
{
    [_zfbButton setSelected:YES];
    [_ybButton setSelected:NO];
    _payType = 0;
}

-(void)ybButtonPick:(id)sender
{
    [_zfbButton setSelected:NO];
    [_ybButton setSelected:YES];
    _payType = 1;
}

-(void)confirmMessge
{
    if ([_countTF.text isEqualToString:@"0"]) {
        _baseAlert = [[UIAlertView alloc]  initWithTitle:@"请选择数量" message:@"票数不能为0哦亲"  delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles: nil];
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
        [_baseAlert show];
        
    } else if ([DeviceUtil isMobileNumber:_phoneTF.text] == NO) {
        _baseAlert = [[UIAlertView alloc]  initWithTitle:@"手机填错了" message:@"手机号格式出错"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
        [_baseAlert show];
        
    } else{
        ConfirmCommonTicketViewController *confirmController = [[ConfirmCommonTicketViewController alloc] init];
        confirmController.phone = _phoneTF.text;
        confirmController.fees = self.fees;
        confirmController.prices = self.price;
        confirmController.count = _countTF.text;
        confirmController.cinemaName = self.cinemaName;
        confirmController.cinemaId = self.cinemaId;
        confirmController.cinemaInfo = _cinemaInfo;
        [self.navigationController pushViewController:confirmController animated:YES];
        [confirmController release];
    }
    
}


- (void) performDismiss: (NSTimer *)timer {
    [_baseAlert dismissWithClickedButtonIndex:0 animated:NO];//important
    [_baseAlert release];
    _baseAlert = NULL;
}


//开始编辑输入框的时候，软键盘出现，执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame = textField.frame;
    int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
    
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
    if(offset > 0)
        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
    
    [UIView commitAnimations];
}


//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}


#pragma mark UITextFieldDelegate
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_phoneTF resignFirstResponder];
}

- (void)onBackgroudTouch
{
    [_phoneTF resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_phoneTF resignFirstResponder];
    return YES;
}

//查看通票
- (void)qryCommontTickets:(BOOL)isSuccess
             commonTicket:(CommonTicketInfo*)cinemaArray
{
    NSInteger type = 0;
    for (int i = 0; i < [cinemaArray.commonCinemaList count]; i++) {
        CommonCinemaInfo *cinemaInfo = [cinemaArray.commonCinemaList objectAtIndex:i];
        if ([cinemaInfo.cinemaid isEqualToString:self.cinemaId]) {
            _cinemaInfo = cinemaInfo;
            type = 1;
            //初始化视图
            [self init_view];
            break;
        }
    }
    if (type == 0) {
        _baseAlert = [[UIAlertView alloc]  initWithTitle:@"不支持电子通票" message:@"不好意思，本场次不支持电子通票"  delegate:self cancelButtonTitle:@"重新选择" otherButtonTitles: nil];
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
        [_baseAlert show];
    }
    
}

@end




